package com.js.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/***
 *
 * @author knight
 * @Date: 2025/10/11 17:28
 * @Description：放行静态资源
 *
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    // 通过 application.yml 配置上传路径
    @Value("${file.upload-path:upload/}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 打印路径用于调试
        System.out.println("静态资源映射路径: file:" + uploadPath);

        // 确保路径以 / 结尾
        String path = uploadPath.endsWith("/") ? uploadPath : uploadPath + "/";

        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:" + path)
                .setCachePeriod(3600) // 缓存1小时
                .resourceChain(true); // 启用资源链
    }
}